package mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import mvp.com.bbc.rcjavaretrofitdaggermvp.app.BaseApplication;

/**
 * 友盟分享回调监听
 * Created by 轻吻旧时光 on 2017/5/13.
 */

public class CustomUMShareListener implements UMShareListener {

    private Activity act;
    private Context context = BaseApplication.getContext();
    private AlertDialog alertDialog;

    public CustomUMShareListener(Activity act) {
        this.act = act;
    }

    public CustomUMShareListener() {
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {
    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
        MyLog.e("Text111", "onResult==" + share_media);
        //ToastUtlis.ToastShow_Short(context, share_media + ToastConstant.share_success);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        if (act != null) {
            if ("QQ".equals(share_media.toString())) {//分享的是QQ
                //equals("错误码：2008 错误信息：没有安装应用 点击查看错误：https://at.umeng.com/ve4Pbm?cid=476"
                if (throwable.getMessage().contains("错误码：2008")) {
                    showPoPWindow("QQ");

                }
            } else if ("WEIXIN".equals(share_media.toString())) {//分享的是微信
                if (throwable.getMessage().contains("错误码：2008")) {
                    showPoPWindow("WEIXIN");

                }
            }
        }
        // ToastUtlis.ToastShow_Short(context, share_media + "++" + ToastConstant.share_fail + "---" + throwable.getMessage());
        if (throwable != null) {
            MyLog.e("throw", "throw:==" + throwable.getMessage());
        }
    }

    private void showPoPWindow(final String type) {
        if (alertDialog != null && alertDialog.isShowing()) {
            return;
        }
        final AlertDialog.Builder dialog = new AlertDialog.Builder(act);
        dialog.setTitle("提示");
        switch (type) {
            case "QQ":
                dialog.setMessage("检测到您未安装QQ客户端，是否去应用市场下载?");
                break;
            case "WEIXIN":
                dialog.setMessage("检测到您未安装微信客户端，是否去应用市场下载?");
                break;
            default:
                break;
        }

        dialog.setPositiveButton("去下载", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
                switch (type) {
                    case "QQ":
                        PackageUtil.startMarket(act, "com.tencent.mobileqq");
                        break;
                    case "WEIXIN":
                        PackageUtil.startMarket(act, "com.tencent.mm");
                        break;
                    default:
                        break;
                }

            }
        });
        dialog.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
            }
        });
        alertDialog = dialog.create();
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        //ToastUtlis.ToastShow_Short(context, share_media + ToastConstant.share_cancel);
        MyLog.e("Text111", "onCancel==" + share_media);
    }
}
